package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.tbit.common.entity.pojo.dto.OutAgeDTO;
import com.tbit.uqbike.constant.Constants;
import com.tbit.uqbike.object.pojo.AccountConfigExt;
import com.tbit.uqbike.object.pojo.QueryConfig;
import com.tbit.uqbike.service.base.RedisService;
import com.tbit.uqbike.service.baseAbstract.SynchronousService;
import com.tbit.uqbike.webmanager.dao.core.AccountConfigDao;
import com.tbit.uqbike.webmanager.dao.core.AccountConfigExtDao;
import com.tbit.uqbike.webmanager.dao.core.DispatchConfigDao;
import com.tbit.uqbike.webmanager.dao.core.OutAgeDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 同步系统配置
 * 注：后续新增表 同步配置时需在此类增加表结构
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2025-2-24
 */
@Service("systemConfigService")
@Slf4j
public class SystemConfigServiceImpl extends SynchronousService {

    @Resource
    private DispatchConfigDao dispatchConfigDao;
    @Resource
    private AccountConfigDao accountConfigDao;
    @Resource
    private AccountConfigExtDao accountConfigExtDao;
    @Resource
    private OutAgeDao outAgeDao;
    @Autowired
    private RedisService redisService;



    @Override
    protected List<?> queryConfig(Integer accountId) {
        OutAgeDTO dto = new OutAgeDTO();
        dto.setAccountId(accountId);
        QueryConfig queryConfig = new QueryConfig(dispatchConfigDao.getByAccountId(accountId),
                accountConfigDao.getById(accountId),
                accountConfigExtDao.getByAccountIdClass(accountId),
                accountConfigExtDao.selectConfig(accountId),
                outAgeDao.queryOutAge(dto)
                );
        return Collections.singletonList(queryConfig);
    }

    /**
     * 删除原区域配置
     * @param accountId 区域id
     */
    @Override
    protected void deleteConfig(Integer accountId) {
        OutAgeDTO dto = new OutAgeDTO();
        dto.setAccountId(accountId);
        QueryConfig queryConfig = new QueryConfig(dispatchConfigDao.getByAccountId(accountId),
                accountConfigDao.getById(accountId),
                accountConfigExtDao.getByAccountIdClass(accountId),
                accountConfigExtDao.selectConfig(accountId),
                outAgeDao.queryOutAge(dto)
        );

        if (ObjectUtil.isNotNull(queryConfig)){
            log.info("删除系统配置:{}", JSON.toJSONString(queryConfig));

            if (ObjectUtil.isNotNull(queryConfig.getDispatchConfig())){
                dispatchConfigDao.delByAccountId(accountId);
            }

            if (ObjectUtil.isNotNull(queryConfig.getAccountConfig())){
                accountConfigDao.delByAccountId(accountId);
            }

            if (CollectionUtil.isNotEmpty(queryConfig.getAccountConfigExtList())){
                AccountConfigExt accountConfigExt = new AccountConfigExt();
                accountConfigExt.setAccountId(accountId);
                accountConfigExtDao.deleteAccountConfigExtById(accountConfigExt);
                redisService.del(Constants.ACCOUNTCONFIG_KEY_PREFIX + accountConfigExt.getAccountId());
            }
            if (ObjectUtil.isNotNull(queryConfig.getMoveConfig())){
                accountConfigExtDao.delByAccountId(accountId);
            }

            if (ObjectUtil.isNotNull(queryConfig.getOutAge())){
                outAgeDao.deleteByAccountId(null, accountId);
            }

        }
    }

    @Override
    protected void insertConfig(List<?> objects, Integer accountId) {
        if (CollectionUtil.isEmpty(objects)){
            return;
        }
        List<QueryConfig> queryConfigList = (List<QueryConfig>) objects;
        QueryConfig queryConfig = queryConfigList.get(0);
        setAccountId(accountId, queryConfig);
        dispatchConfigDao.insert(queryConfig.getDispatchConfig());
        accountConfigDao.insert(queryConfig.getAccountConfig());
        accountConfigExtDao.addBatch(queryConfig.getAccountConfigExtList());
        accountConfigExtDao.insertMaintainMachineMoveConfig(queryConfig.getMoveConfig());
        outAgeDao.insert(queryConfig.getOutAge());
    }


    /**
     * 设置区域id
     * @param accountId 区域id
     * @param queryConfig  系统配置
     */
    private static void setAccountId(Integer accountId, QueryConfig queryConfig) {
        queryConfig.getDispatchConfig().setAccountId(accountId);
        queryConfig.getAccountConfig().setAccountId(accountId);
        queryConfig.getMoveConfig().setAccountId(accountId);
        if (Objects.nonNull(queryConfig.getOutAge())){
            queryConfig.getOutAge().setAccountId(accountId);
        }
        queryConfig.getAccountConfigExtList().forEach(AccountConfigExt -> AccountConfigExt.setAccountId(accountId));
    }
}
